package com.manpower.ui;

import java.util.ArrayList;
import java.util.HashMap;

import com.manpower.Receiver;
import com.manpower.utl.LocationController;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class FeatureListActivity extends AnotherBaseActivity {

	private GridView mFeatureList;
	private final String GRIDVIEW_TITLE = "item_title";
	private final String GRIDVIEW_IMG = "item_img";
	private final int[] mItemName = { R.string.label_EnRegistration,
			R.string.label_uncheck_file, R.string.label_report_state,
			R.string.label_regional_statistical, R.string.label_manual };
	private final Object[] mItemImage = { R.drawable.grid_item_enregist_style,
			R.drawable.grid_item_uncheckfile_style,
			R.drawable.grid_item_report_style,
			R.drawable.grid_item_regionstat_style,
			R.drawable.grid_item_manual_style };
	private ArrayList<HashMap<String, Object>> mDatas;

	private final int ENREGISTRATION = 0;
	private final int UNCHECK_FILE = 1;
	private final int REPORT_STATE = 2;
	private final int REGIONAL_STATISTICAL = 3;
	private final int MANUAL = 4;

	TelephonyManager mTelePhonyMgr;
	private Receiver receiver;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_feature_list);
		initiate();
		updateView();
		LocationController.getInstance(this).startLocate();
		receiver = Receiver.registerReceiver(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.findItem(R.id.menu_base_navigation_Report).setVisible(false);
		menu.findItem(R.id.menu_base_navigation_recall).setVisible(false);
		menu.findItem(R.id.menu_base_navigation_check).setVisible(false);
		menu.findItem(R.id.menu_base_navigation_back).setEnabled(true);
		menu.findItem(R.id.menu_base_navigation_exit).setEnabled(true);

		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_base_navigation_exit:
		case R.id.menu_base_navigation_back:
			ExitDialog dialog = new ExitDialog(this, R.style.custom_dialog);
			dialog.show();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	private void initiate() {
//		txtTitle.setText(R.string.title_login);
//		btnBack = (ImageView) this.findViewById(R.id.btnBack);
//		btnBack.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View arg0) {
//				new ExitDialog(FeatureListActivity.this, R.style.custom_dialog)
//						.show();
//			}
//		});
		mDatas = new ArrayList<HashMap<String, Object>>();
		mFeatureList = (GridView) findViewById(R.id.grid_list);
		mFeatureList.setSelector(new ColorDrawable(Color.TRANSPARENT));
		mFeatureList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = null;
				switch (arg2) {
				case ENREGISTRATION:
					intent = new Intent(FeatureListActivity.this,
							EnRegistrationItemActivity.class);
					break;
				case REGIONAL_STATISTICAL:
					intent = new Intent(FeatureListActivity.this,
							RegionalStatisticalActivity.class);
					break;
				case UNCHECK_FILE:
					intent = new Intent(FeatureListActivity.this,
							UnCheckFileActivity.class);
					break;
				case MANUAL:
					intent = new Intent(FeatureListActivity.this,
							ManualActivity.class);
					break;
				case REPORT_STATE:
					intent = new Intent(FeatureListActivity.this,
							ReportStatusActivity.class);
					break;
				}
				if (intent != null) {
					FeatureListActivity.this.startActivity(intent);
				}

			}

		});
	}

	private void updateView() {
		for (int i = 0; i < mItemName.length; i++) {
			HashMap<String, Object> hashMap = new HashMap<String, Object>();
			hashMap.put(GRIDVIEW_IMG, mItemImage[i]);
			hashMap.put(GRIDVIEW_TITLE,
					this.getResources().getString(mItemName[i]));
			mDatas.add(hashMap);
		}
		SimpleAdapter mAdapter = new SimpleAdapter(this, mDatas,
				R.layout.feature_list_item, new String[] { GRIDVIEW_IMG,
						GRIDVIEW_TITLE }, new int[] { R.id.item_img,
						R.id.item_title });
		mFeatureList.setAdapter(mAdapter);
	}

//	@Override
//	public boolean dispatchKeyEvent(KeyEvent event) {
//		if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK)) {
//			new ExitDialog(FeatureListActivity.this, R.style.custom_dialog)
//					.show();
//			return true;
//		}
//		return super.dispatchKeyEvent(event);
//	}


}
